django urlpatterns and url convert
When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute:
-
Django determines the root URLconf module to use. Ordinarily, this is the value of the
ROOT_URLCONFsetting, but if the incomingHttpRequestobject has aurlconfattribute (set by middleware), its value will be used in place of theROOT_URLCONFsetting. -
Django loads that Python module and looks for the variable
urlpatterns. This should be a sequence ofdjango.urls.path()and/ordjango.urls.re_path()instances. -
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against
path_info. -
Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view). The view gets passed the following arguments:
-
An instance of
HttpRequest. -
If the matched URL pattern contained no named groups, then the matches from the regular expression are provided as positional arguments.
-
The keyword arguments are made up of any named parts matched by the path expression that are provided, overridden by any arguments specified in the optional
kwargsargument todjango.urls.path()ordjango.urls.re_path().Changed in Django 3.0:
In older versions, the keyword arguments with
Nonevalues are made up also for not provided named parts.
-
-
If no URL pattern matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view. See Error handling below.